package com.flashcash.kotlin.activity

import android.app.Activity
import android.content.Intent
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageButton
import android.widget.TextView
import com.flashcash.kotlin.fragment.PersonalInfoFragment
import com.flashcash.kotlin.fragment.ProfessionalInfoFragment
import com.flashcash.kotlin.fragment.certfication.IdentifyFragment
import com.flashcash.solu.R
import com.flashcash.solu.app.base.BaseActivity
import com.flashcash.solu.app.base.BaseActivityView
import com.flashcash.solu.app.base.presenter.BaseActivityPresenter
import com.flashcash.solu.app.base.presenter.BaseActivityPresenterImpl
import com.flashcash.solu.view.IntroduceActivity
import com.flashcash.solu.view.certification.ExampleActivity
import com.flashcash.solu.widget.stepview.HorizontalStepView
import com.flashcash.solu.widget.stepview.bean.StepBean
import org.jetbrains.anko.find
import org.jetbrains.anko.sdk25.coroutines.onClick
import java.util.*

/**
 * Created by XLEO on 2018/1/18.
 */
class InfoFillActivity : BaseActivity<InfoFillPresenter>(), InfoFillView {
    override fun getLayoutResId(): Int {
        return R.layout.activity_infofill
    }

    override fun onBackPressed() {
        super.onBackPressed()
        identifyFragment?.onBackPressed()

        reback()
    }

    override fun init() {
        find<ImageButton>(R.id.id_imagebutton_back).onClick {
            identifyFragment?.onBackPressed()
            setResult(Activity.RESULT_CANCELED)
            if (identifyFragment?.fromSplash!!) {
                startActivity(Intent(baseActivity, IntroduceActivity::class.java))
            }
            finish()
        }
        find<TextView>(R.id.id_textview_title).text = "Identification"
        find<ImageButton>(R.id.id_imagebutton_info_list).visibility = View.INVISIBLE
        find<FrameLayout>(R.id.fl_container)
//        val get = intent.extras.get(FieldParams.INFO_NO)
//        if (get != null && (get as Int) in 0..3) {
//            transTo(get as Int)
//        } else {
//            transTo(0)
//        }
        transTo(0)
    }

    private var currentPosition: Int = -1//当前Fragment
    var identifyFragment: IdentifyFragment? = null
    var professionalInfoFragment: ProfessionalInfoFragment? = null
    //TODO 记得在这里修改stepView状态
    private fun transTo(i: Int) {
        val beginTransaction = supportFragmentManager.beginTransaction()
        beginTransaction.setCustomAnimations(R.anim.abc_fade_in, R.anim.abc_fade_out)
        when (i) {
            0 -> {
                identifyFragment = IdentifyFragment()
                beginTransaction.replace(R.id.fl_container, identifyFragment, "Identify")
            }
            1 -> {
                val personalInfoFragment = PersonalInfoFragment()
                personalInfoFragment.setStepViewGone()
                beginTransaction.replace(R.id.fl_container, personalInfoFragment, "Personal")
            }
            2 -> {
                val professionalInfoFragment = ProfessionalInfoFragment()
                professionalInfoFragment.setStepViewGone()
                beginTransaction.replace(R.id.fl_container, professionalInfoFragment, "Contact")
            }
            3 -> {
                professionalInfoFragment = ProfessionalInfoFragment()
                professionalInfoFragment!!.setStepViewGone()
                beginTransaction.replace(R.id.fl_container, professionalInfoFragment, "Job")
            }
            else -> {
            }
        }
        beginTransaction.commit()
        currentPosition = i
        changeMarkPosition(i)
    }

    fun forward() {
        if (currentPosition != -1 && currentPosition < 3) {
            transTo((currentPosition + 1) % 4)
        } else if (currentPosition == 3) {
            setResult(Activity.RESULT_OK)
            finish()
        }
    }

    fun reback() {
        if (currentPosition != -1 && currentPosition > 0) {
            transTo((currentPosition - 1) % 4)
        } else if (currentPosition == 0) {
            setResult(Activity.RESULT_CANCELED)
            if (identifyFragment?.fromSplash!!) {
                startActivity(Intent(baseActivity, IntroduceActivity::class.java))
            }
            finish()
        }
    }

    private var stepView: HorizontalStepView? = null
    /**
     * 设置StepView节点状态
     * @param i 当前步骤
     */
    private fun changeMarkPosition(i: Int) {
        /*find<TextView>(R.id.tv_identify).isSelected = false
        find<TextView>(R.id.tv_personal).isSelected = false
        find<TextView>(R.id.tv_contact).isSelected = false
        find<TextView>(R.id.tv_professional).isSelected = false

        find<TimeLineView>(R.id.identify_node).currentStatus = TimeLineView.INACTIVE
        find<TimeLineView>(R.id.personal_node).currentStatus = TimeLineView.INACTIVE
        find<TimeLineView>(R.id.contact_node).currentStatus = TimeLineView.INACTIVE
        find<TimeLineView>(R.id.job_node).currentStatus = TimeLineView.INACTIVE
        when (i) {
            0 -> {
                find<TextView>(R.id.tv_identify).isSelected = true
                find<TimeLineView>(R.id.identify_node).currentStatus = TimeLineView.ACTIVE
            }
            1 -> {
                find<TextView>(R.id.tv_personal).isSelected = true
                find<TimeLineView>(R.id.personal_node).currentStatus = TimeLineView.ACTIVE
            }
            2 -> {
                find<TextView>(R.id.tv_contact).isSelected = true
                find<TimeLineView>(R.id.contact_node).currentStatus = TimeLineView.ACTIVE
            }
            3 -> {
                find<TextView>(R.id.tv_professional).isSelected = true
                find<TimeLineView>(R.id.job_node).currentStatus = TimeLineView.ACTIVE
            }
            else -> {
            }
        }*/
        stepView = find(R.id.step_view)
        val stepsBeanList = ArrayList<StepBean>()
        val stepBean0 = StepBean("Identification", StepBean.STEP_UNDO)
        val stepBean1 = StepBean("Personal Info", StepBean.STEP_UNDO)
        val stepBean2 = StepBean("Contact Info", StepBean.STEP_UNDO)
        val stepBean3 = StepBean("Job Info", StepBean.STEP_UNDO)

        when (i) {
            0 -> {
                stepBean0.state = StepBean.STEP_CURRENT
            }
            1 -> {
                stepBean0.state = StepBean.STEP_COMPLETED
                stepBean1.state = StepBean.STEP_CURRENT
            }
            2 -> {
                stepBean0.state = StepBean.STEP_COMPLETED
                stepBean1.state = StepBean.STEP_COMPLETED
                stepBean2.state = StepBean.STEP_CURRENT
                stepBean3.state = StepBean.STEP_UNDO
            }
            3 -> {
                stepBean0.state = StepBean.STEP_COMPLETED
                stepBean1.state = StepBean.STEP_COMPLETED
                stepBean2.state = StepBean.STEP_COMPLETED
                stepBean3.state = StepBean.STEP_CURRENT
            }
            else -> {
            }
        }
        stepsBeanList.add(stepBean0)
        stepsBeanList.add(stepBean1)
        stepsBeanList.add(stepBean2)
        stepsBeanList.add(stepBean3)

        stepView!!.setStepViewTexts(stepsBeanList)
    }

    override fun initPresenterImpl(): InfoFillPresenter {
        return InfoFillPresenterImpl()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        //相机下一步回调
        if (IdentifyFragment.EXAMPLE_REQUEST_CODE == requestCode && ExampleActivity.EXAMPLE_RESULT_CODE == resultCode) {
            if (identifyFragment != null) {
                identifyFragment!!.openCamera()
            }
        } else if (ProfessionalInfoFragment.EXAMPLE_REQUEST_CODE == requestCode && ExampleActivity.EXAMPLE_RESULT_CODE == resultCode) {
            if (professionalInfoFragment != null) {
                professionalInfoFragment!!.openCamera()
            }
        }
    }

}

class InfoFillPresenterImpl : InfoFillPresenter, BaseActivityPresenterImpl() {

}

interface InfoFillView : BaseActivityView {

}

interface InfoFillPresenter : BaseActivityPresenter {

}
